<template>
  <div>
    <el-dialog
      :title="form.id ? '分类编辑' : '分类添加'"
      :visible.sync="buffer.isShow"
    >
      <el-form :model="form">
        {{ form }}
        <!-- 顶级分类区域 -->
        <el-form-item label="分类菜单" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择顶级分类">
            <el-option label="分类菜单" :value="0"></el-option>
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 分类名称 -->
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 分类图片 -->
        <el-form-item label="分类图片" :label-width="formLabelWidth">
        <el-upload
            class="avatar-uploader"
            action="#"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="changeFile"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
        <!-- 分类状态 -->
        <el-form-item label="分类状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="buffer.isShow = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["buffer", "cateList"],
  watch: {
    buffer: {
      handler(newV, oldV) {
        //2.7赋空表单数据
        if (!newV.isShow) {
            //监听窗口关闭
            this.form = {
            pid: "", //父级分类id
            catename: "", //分类名称
            img: "", //分类图片
            status: 1, //菜单状态 1 2
            };
            //重置分类图片
            this.imageUrl =""
        }
      },
      deep: true,
    },
  },
  data() {
    return {
      form: {
        pid: "", //父级分类id
        catename: "", //分类名称
        img: "", //分类图片
        status: 1, //菜单状态 1 2
      },

        dialogTableVisible: false,
        dialogFormVisible: true,

        formLabelWidth: "120px",
        imageUrl:"",//分类展示图片路径
    };
  },
  methods: {
    //1.监听图片选中
    changeFile(info){
        
        //1.1结构获取图片信息
        let {name,size} = info.raw
        // console.log(name,size)
        //验证图片大小
        if(size > 1024 * 1024 * 2){
            this.$message({type:"warning",message:"图片大小受限!"})
        }

        //1.3验证图片格式  后缀suffix
        let allowSuffix = ["png","jpg","jpeg","gif"]
        if(!allowSuffix.includes(name.split('.').pop())){
            this.$message({type:"warning",message:"图片格式有误！"})
        }

        //1.4将用户选择的分类图片展示到页面上
        this.imageUrl = URL.createObjectURL(info.raw)

        //1.5存储需要提交的图片数据
        this.form.img = info.raw
    },
    //2.提交新增菜单数据
    submit() {
      //2.1获取提交数据
      //2.2检验数据格式
      //2.3执行提交操作
      (this.form.id
        ? this.$api.reqCateEdit(this.form)
        : this.$api.reqCateAdd(this.form)
      )
        .then((res) => {
          //2.4关闭弹窗
          this.buffer.isShow = false;
          //2.5提示用户操作成功
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
          });
          //2.6更新菜单列表数据
          //子传父通过
          //this.$emit ('父亲传递的自定义事件名字','传递的数据')
          this.$emit("channel", "ok");
          //2.7赋空表单
        })
        .catch((err) => console.log(err));
    },
    
  },
};

</script>

<style lang="less" scoped>

 /deep/.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /deep/.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  /deep/.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  /deep/.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
